﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.Dictionary;
using Base.Core.UnityFactory;
using BF.Model;
using BF.Infrastructure;
using System.Web.UI.HtmlControls;

namespace BF.Web.pages.Dictionary.DictManagement
{
    public partial class DictList : BasePage
    {
        Idic_DictBLL bll = IOCFactory.GetIOCResolve<Idic_DictBLL>();//BLL

        #region 系统事件

        /// <summary>
        /// PageLoad
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            anpDict.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            anpDict.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 停用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnStop_Click(object sender, EventArgs e)
        {
            int iflag = int.Parse(hideStop.Value);
            for (int i = 0; i < rptDict.Items.Count; i++)
            {
                HtmlInputCheckBox chkGKey = (HtmlInputCheckBox)rptDict.Items[i].FindControl("chkGKey");
                if (chkGKey != null && chkGKey.Checked)
                {
                    dic_Dict tempModel = bll.GetModel(new Guid(chkGKey.Value));
                    tempModel.iUse = iflag;
                }
            }

            if (bll.Update())
            {
                //更新标准字典缓存
                BF.Web.App_Code.UICommon.UpdateDictList();       
                Messages(iflag == 1 ? "启用成功！" : "停用成功！");
            }
            else
            {
                Messages(iflag == 1 ? "启用失败！" : "停用失败！");
            }

            anpDict.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void anpDict_PageChanged(object sender, EventArgs e)
        {
            GetValue(anpDict.PageSize, anpDict.CurrentPageIndex);
        }
        #endregion

        #region 自定义方法
        protected void GetValue(int pageSize, int currentPageIndex)                     //初始化数据
        {
            //查询条件：表编码、表名称 模糊查询
            Func<dic_Dict, bool> linqWhere = p =>
                                    (string.IsNullOrEmpty(tbxSearch.Text.Trim()) ? true : p.sCode.Contains(tbxSearch.Text.Trim())) &&
                                    (string.IsNullOrEmpty(txtName.Text.Trim()) ? true : p.sName.Contains(txtName.Text.Trim())) &&
                                     p.sTableCode == "GEN";

            //分页
            anpDict.RecordCount = BF.Web.App_Code.UICommon.DictList.Where(linqWhere).Count();
            var modelList = BF.Web.App_Code.UICommon.DictList
                                    .Where(linqWhere)
                                    .OrderBy(p => p.sCode)
                                    .Skip(new PageModel(currentPageIndex).StartRecord)
                                    .Take(new PageModel(currentPageIndex).PageSize)
                                    .ToList<Model.dic_Dict>();
            //数据绑定
            rptDict.DataSource = modelList;
            rptDict.DataBind();
        }
        #endregion
    }
}